//
//  UserRewardCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/1.
//

import UIKit

class UserRewardCell: UITableViewCell {
    var bkgView: UIView!
    var imgView: UIImageView!
    var titleLab: UILabel!
    var detailLab: UILabel!
    var tipLab: UILabel!
    var timeLab: UILabel!
    var statusBtn: UIButton!  //过期
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        bkgView = UIView()
        bkgView.backgroundColor = .white
        contentView.addSubview(bkgView)
        
        imgView = UIImageView()
        imgView.backgroundColor = Theme.lineColor
        bkgView.addSubview(imgView)
        
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = UIColor.clear
        titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        titleLab.numberOfLines = 0
        titleLab.lineBreakMode = .byTruncatingTail
        titleLab.textAlignment = .left
        bkgView.addSubview(titleLab)
        
        detailLab = UILabel()
        detailLab.textColor = Theme.titleLabColor
        detailLab.backgroundColor = UIColor.clear
        detailLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        detailLab.numberOfLines = 0
        detailLab.lineBreakMode = .byTruncatingTail
        detailLab.textAlignment = .left
        bkgView.addSubview(detailLab)
        
        tipLab = UILabel()
        tipLab.textColor = Theme.titleLabColor
        tipLab.backgroundColor = UIColor.clear
        tipLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        tipLab.numberOfLines = 1
        tipLab.lineBreakMode = .byTruncatingTail
        tipLab.textAlignment = .center
        tipLab.layer.cornerRadius = 11
        tipLab.layer.masksToBounds = true
        tipLab.layer.borderWidth = 1
        tipLab.layer.borderColor = UIColor.init(hex: 0x939AA8).cgColor
        bkgView.addSubview(tipLab)
        
        timeLab = UILabel()
        timeLab.textColor = Theme.placeholderColor
        timeLab.backgroundColor = UIColor.clear
        timeLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        timeLab.numberOfLines = 0
        timeLab.lineBreakMode = .byTruncatingTail
        timeLab.textAlignment = .right
        bkgView.addSubview(timeLab)
        
        statusBtn = UIButton()
        statusBtn.setup_titleColor(color: Theme.placeholderColor)
        //statusBtn.backgroundColor = Theme.tfBorderColor
        statusBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        statusBtn.isHidden = true
//        statusBtn.setup_backgroundImage(image: UIImage.)
        statusBtn.setup_backgroundImage(image: self.createStatusBkgView())
        bkgView.addSubview(statusBtn)
        
        
        
        
//        titleLab.text = "中秋活动优惠券满1000减200"
//        detailLab.text = "满减券说明文字，满减券说明文字满减券说明"
//        tipLab.text = "仅商品可用"
//        timeLab.text = "2023-08-10至2023-08-3"
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(0)
            make.right.equalTo(-14)
            make.bottom.equalTo(-10).priority(.high)
        }
        
        imgView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(14)
            make.width.equalTo(0)
            make.height.equalTo(0)
            make.bottom.lessThanOrEqualTo(-14)
        }
        
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(14)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-70)
        }
        
        detailLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(titleLab.snp.bottom).offset(4)
            make.right.equalTo(-10)
        }
        
        tipLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(detailLab.snp.bottom).offset(10)
            make.width.greaterThanOrEqualTo(40)
            make.height.equalTo(22)
        }
        
        timeLab.snp.makeConstraints { make in
            make.left.equalTo(tipLab.snp.right)
            make.right.equalTo(-10)
            make.top.equalTo(detailLab.snp.bottom).offset(10)
            make.height.equalTo(22)
            make.bottom.lessThanOrEqualTo(-16)
        }
        
        statusBtn.snp.makeConstraints { make in
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(26)
            make.width.greaterThanOrEqualTo(60)
        }
        
        self.layoutIfNeeded()
        self.createShadowLayer()
    }
    
    private func createShadowLayer(){
        bkgView.layer.masksToBounds = false
        //设置边框圆角
        bkgView.layer.cornerRadius = 8
        //设置阴影颜色
        bkgView.layer.shadowColor = UIColor.init(hex: 0xF4F4F9, alpha: 0.9).cgColor
        //设置透明度
        bkgView.layer.shadowOpacity = 1
        //设置阴影半径
        bkgView.layer.shadowRadius = 10
        //设置阴影偏移量
        bkgView.layer.shadowOffset = CGSize(width: 0, height: 0)
    }
    
    ///优惠券类型 couponType：0商城满减券，1礼品券
    func setUserRewardCell(data: CouponDataModel) {
        if (data.giftName ?? "") != "" {
            //礼品券
            self.titleLab.text = data.giftName ?? ""
        }else{
            //商品券
            self.titleLab.text = data.couponInfo ?? ""
        }
        
        if let pickerUrl = URL.init(string: data.urlPicture ?? "") {
            self.imgView.isHidden = false
            self.imgView.kf.setImage(with: pickerUrl, placeholder: UIImage.init(named: "icon_default_place_v"))
            self.imgView.snp.updateConstraints { make in
                make.height.equalTo(100)
                make.width.equalTo(100)
            }
        }else{
            self.imgView.isHidden = true
            self.imgView.snp.updateConstraints { make in
                make.height.equalTo(0)
                make.width.equalTo(0)
            }
        }
        
        self.detailLab.text = data.description ?? ""
        self.timeLab.text = data.validTime ?? ""
        
        let tipTxt = XTManageCenter.getCouponuByscene(useScene: data.useScene ?? "")
        if tipTxt != "" {
            self.tipLab.isHidden = false
            self.tipLab.text = tipTxt
        }else{
            self.tipLab.isHidden = true
        }
        
        self.tipLab.frame.size.width = 200
        self.tipLab.sizeToFit()
        let tempW = self.tipLab.frame.size.width + 20
        self.tipLab.snp.updateConstraints { make in
            make.width.greaterThanOrEqualTo(tempW)
        }
        
        switch( data.status ?? 0) {
            case 1:
                //已使用
                self.statusBtn.isHidden = false
                self.statusBtn.setup_titleLabel(text: MMLocalizedString("coupon_has_used", comment: "已使用"))
            case 2:
                //已过期
                self.statusBtn.isHidden = false
                self.statusBtn.setup_titleLabel(text: MMLocalizedString("coupon_expired", comment: "已过期"))
            default:
                //未使用未过期
                self.statusBtn.isHidden = true
        }
        
        self.statusBtn.titleLabel?.frame.size.width = 200
        self.statusBtn.titleLabel?.sizeToFit()
        let status_width = (self.statusBtn.titleLabel?.frame.size.width ?? 0) + 16
        self.statusBtn.snp.updateConstraints { make in
            make.width.greaterThanOrEqualTo(status_width)
        }
        self.updateConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    ///创建优惠券状态圆弧背景
    private func createStatusBkgView() -> UIImage {
        let tempView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 40, height: 20))
        tempView.backgroundColor = Theme.tfBorderColor
        tempView.round(byRoundingCorners: UIRectCorner.init(arrayLiteral: .bottomLeft, .topRight), cornerRadi: 8)
        
        UIGraphicsBeginImageContext(tempView.bounds.size);
        tempView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let img = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();
        return img
    }
}
